package com.jxLC.Action;

import java.io.File;
import java.io.IOException;


import javax.servlet.ServletContext;


import org.apache.struts2.ServletActionContext;
import org.apache.struts2.util.ServletContextAware;

import com.jxLC.Model.Content;
import com.jxLC.Model.Filecontent;
import com.jxLC.Service.ContentService;
import com.jxLC.Service.FilecontentService;
import com.jxLC.Service.UploadService;
import com.opensymphony.xwork2.ActionSupport;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UploadAction extends ActionSupport   implements ServletContextAware{
	
	private static final long serialVersionUID = -897836913175252741L;
	
	/*
	 * 批量上传 图片
	 */
	
	private File image1;
	private File image2;
	private File image3;
	private String image1FileName;
	private String image2FileName;
	private String image3FileName;
	
	
	private String imageSavePath;
	private String tip;

	
	
	/*
	 * 单个上传word文档
	 */
	private File upload;
	private String uploadContentType;
	private String uploadFileName;
	private String savePath;
	

	/*
	 * 管理员上传
	 */
	private String CContent_author;
	private String CContent_time;
	private int CTopic_ID;
	private ContentService contentService=new ContentService();
	private UploadService uploadService=new UploadService();
	private String  CContent_topic;
	private Content content;
	
	
	
	/*
	 * 用户上传
	 */
	private Filecontent filecontent;
	private FilecontentService filecontentService=new FilecontentService();
	private String CNum;
	private String CFC_time;
	private String CFC_topic;
	private int CFile_id;
	private ServletContext context;
	
	
	
	

	 
	/** 

	* 替换文件上传中出现的错误信息 
	* 
	* */

	@SuppressWarnings("unused")
	@Override 

	public void addActionError(String anErrorMessage) { 

	//这里要先判断一下，是我们要替换的错误，才处理 

	if (anErrorMessage.startsWith("the request was rejected because its size")) { 

	Matcher m = Pattern.compile("//d+").matcher(anErrorMessage); 

	String s1 = ""; 

	if (m.find()) s1 = m.group(); 

	String s2 = ""; 

	if (m.find()) s2 = m.group(); 

	//偷梁换柱，将信息替换掉 
	//super.addActionError("你上传的文件大小（" + s1 + "）超过允许的大小（" + s2 + "）"); 
	//也可以改为在Field级别的错误
	 super.addFieldError("file","图片上传失败,您上传的图片大小超过允许的大小。请逐一上传图片！"); 

	} else {//否则按原来的方法处理 

	super.addActionError(anErrorMessage); 
	} 

	} 

	
	/*
	 * 用户上传
	 */
	public String validateUserUpload(){
		if(this.getUpload()==null){
			this.addFieldError("error", "请选择你要上传的文件");
			return INPUT;
		}/*else{
		 if(!(this.getUploadContentType().equals("application/msword"))){
			 this.addFieldError("error", "您要上传的文件类型不正确,系统只允许上传word文档");
		    	return INPUT;
		    }
		}*/
		return SUCCESS;
	}
	
	public String userUpload(){
		filecontent=new Filecontent();
		filecontent.setCNum(CNum);
		filecontent.setCFC_time(this.getCFC_time());
		filecontent.setCFC_topic(this.getCFC_topic());
		filecontent.setCFile_id(this.getCFile_id());
		System.out.println("cfcid="+filecontent.getCFC_id());
		String realpath=context.getRealPath(this.getSavePath());  // realpath是upload文件夹 的绝对路径
		String result=this.uploadService.setText(realpath, this.getUpload(),this.getUploadFileName());
		if(result.equals("error")){
			ServletActionContext.getRequest().setAttribute("errorMessage", "文件上传出错");
			return ERROR;
		}else{
			filecontent.setCFC_text(result);
		}
		if(this.filecontentService.add(filecontent,null, null)){
			return SUCCESS;
		}else{
			return ERROR;
		}
		
	}
	public String validateAdminUpload(){
		if(this.getUpload()==null){
			this.addFieldError("error", "请选择你要上传的文件");
			return INPUT;
		}else{
			System.out.println("type="+this.getUploadContentType());
		 if(!(this.getUploadContentType().equals("application/msword"))){
			 this.addFieldError("error", "您要上传的文件类型不正确,系统只允许上传word文档");
		     return INPUT;
		    }
		}
		return SUCCESS;
	}
	
	
//	/*
//	 * 管理员上传
//	 */
//	public String  adminUpload() throws Exception{
//		content=new Content();
//		content.setCNum(this.getCNum());
//		content.setCContent_time(this.getCContent_time());
//		content.setCTopic_ID(this.getCTopic_ID());
//		content.setCContent_topic(this.getCContent_topic());
//		
//		System.out.println("!!!"+this.getUploadContentType());
//		String realpath=context.getRealPath(this.getSavePath());            // realpath是upload文件夹 的绝对路径
//		String result=this.uploadService.setText(realpath, this.getUpload(),this.getUploadFileName());
//		
//		if(result.equals("error")){
//			ServletActionContext.getRequest().setAttribute("errorMessage", "文件上传出错");
//			return ERROR;
//		}else{
//			content.setCContent_text(result);
//		}
//		
//		if(this.contentService.add(content)){
//			return SUCCESS;
//		}else{
//			return ERROR;
//		}
//	    	
//	}
	
	
	/*
	 * 图片上传
	 */
	
	public String imageUpload() throws IOException{
	    String realpath=context.getRealPath(this.getImageSavePath());
	      if(this.getImage1()!=null){
	    	  if(this.uploadService.batchUpload(realpath, this.getImage1(),this.getImage1FileName(),0).equals("error")){
	    		  ServletActionContext.getRequest().setAttribute("tip", "图片上传出错");
		      }
	      }
	      if(this.getImage2()!=null){
	    	  if(this.uploadService.batchUpload(realpath, this.getImage2(),this.getImage2FileName(),1).equals("error")){
	    		  ServletActionContext.getRequest().setAttribute("tip", "图片上传出错");
		      }
	      }
	      if(this.getImage3()!=null){
	    	  if(this.uploadService.batchUpload(realpath, this.getImage3(),this.getImage3FileName(),2).equals("error")){
	    		  ServletActionContext.getRequest().setAttribute("tip", "图片上传出错");
		      }
	      }
	      ServletActionContext.getRequest().setAttribute("tip", "图片上传成功");
	      return SUCCESS;
	}
	
	
	
	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}


	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getSavePath() {
		return savePath;
	}


	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	

	public String getCContent_author() {
		return CContent_author;
	}

	public void setCContent_author(String cContentAuthor) {
		CContent_author = cContentAuthor;
	}


	public String getCContent_time() {
		return CContent_time;
	}



	public void setCContent_time(String cContentTime) {
		CContent_time = cContentTime;
	}

	public int getCTopic_ID() {
		return CTopic_ID;
	}

	public void setCTopic_ID(int cTopicID) {
		CTopic_ID = cTopicID;
	}

	public ContentService getContentService() {
		return contentService;
	}

	public void setContentService(ContentService contentService) {
		this.contentService = contentService;
	}

	public UploadService getUploadService() {
		return uploadService;
	}

	public void setUploadService(UploadService uploadService) {
		this.uploadService = uploadService;
	}

	public String getCContent_topic() {
		return CContent_topic;
	}

	public void setCContent_topic(String cContentTopic) {
		CContent_topic = cContentTopic;
	}

	public Content getContent() {
		return content;
	}

	public void setContent(Content content) {
		this.content = content;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public void setContext(ServletContext context) {
		this.context = context;
	}

	public void setServletContext(ServletContext context) {
		   this.context = context;
	}
	public ServletContext getContext() {
		return context;
	}
	public String getImageSavePath() {
		return imageSavePath;
	}
	public void setImageSavePath(String imageSavePath) {
		this.imageSavePath = imageSavePath;
	}
	public Filecontent getFilecontent() {
		return filecontent;
	}
	public void setFilecontent(Filecontent filecontent) {
		this.filecontent = filecontent;
	}
	
	public String getCNum() {
		return CNum;
	}

	public void setCNum(String cNum) {
		CNum = cNum;
	}

	public String getCFC_time() {
		return CFC_time;
	}
	public void setCFC_time(String cFCTime) {
		CFC_time = cFCTime;
	}
	public String getCFC_topic() {
		return CFC_topic;
	}
	public void setCFC_topic(String cFCTopic) {
		CFC_topic = cFCTopic;
	}
	public int getCFile_id() {
		return CFile_id;
	}
	public void setCFile_id(int cFileId) {
		CFile_id = cFileId;
	}
	
	
	public FilecontentService getFilecontentService() {
		return filecontentService;
	}
	public void setFilecontentService(FilecontentService filecontentService) {
		this.filecontentService = filecontentService;
	}

	public File getImage1() {
		return image1;
	}

	public void setImage1(File image1) {
		this.image1 = image1;
	}

	public File getImage2() {
		return image2;
	}

	public void setImage2(File image2) {
		this.image2 = image2;
	}

	public File getImage3() {
		return image3;
	}

	public void setImage3(File image3) {
		this.image3 = image3;
	}

	public String getImage1FileName() {
		return image1FileName;
	}

	public void setImage1FileName(String image1FileName) {
		this.image1FileName = image1FileName;
	}

	public String getImage2FileName() {
		return image2FileName;
	}

	public void setImage2FileName(String image2FileName) {
		this.image2FileName = image2FileName;
	}

	public String getImage3FileName() {
		return image3FileName;
	}

	public void setImage3FileName(String image3FileName) {
		this.image3FileName = image3FileName;
	}

	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}
    
	
	
    
    
	
	
    


	
	
	

	
	
}
